/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*-  */
/*
 * catchit
 * Copyright (C) 2013 Ryan Grinkewitz <graphite@Toto>
	 * 
 */

#include "event-manager.h"


EventManager* EventManager::eventManagerInstance = NULL; 
	 

EventManager* EventManager::getInstance()
{

	if (!eventManagerInstance){ 
		eventManagerInstance = new EventManager;
		srand(time(NULL));
	}
	 

	return eventManagerInstance;
}

void EventManager::processEvent(){
	eventAction = DO_NOTHING;
	if( SDL_PollEvent( &event ) ) {
		handleEvent(&event);
	}
}

void EventManager::handleEvent(SDL_Event* keyboard_event){

	
	if( keyboard_event->type == SDL_KEYDOWN ) {
		switch( keyboard_event->key.keysym.sym ) { 
			case SDLK_w: 
				eventAction = JUMP_UP;
				break; 
			case SDLK_LEFT: 
				eventAction = MOVE_LEFT;
				break; 
			case SDLK_RIGHT: 
				eventAction = MOVE_RIGHT;
				break; 
			case SDLK_q: 
				eventAction = JUMP_LEFT;
				break; 
			case SDLK_e: 
				eventAction = JUMP_RIGHT;
				break; 			
			case SDLK_UP: 
				eventAction = MOVE_UP;
				break; 
			case SDLK_DOWN: 
				eventAction = MOVE_DOWN;
				break; 					
		}

	}else if( keyboard_event->type == SDL_QUIT ) { 
		eventAction = QUIT; 
	} else if(keyboard_event->type == SDL_KEYUP ) {
		switch( keyboard_event->key.keysym.sym ) { 
		//	case SDLK_w: 
		//		eventAction = JUMP_UP;
		//		break; 
			case SDLK_LEFT: 
				eventAction = END_MOVE_LEFT;
				break; 
			case SDLK_RIGHT: 
				eventAction = END_MOVE_RIGHT;
				break; 
		//	case SDLK_q: 
		//		eventAction = JUMP_LEFT;
		//		break; 
		//	case SDLK_e: 
		//		eventAction = JUMP_RIGHT;
		//		break; 			
			case SDLK_UP: 
				eventAction = END_MOVE_UP;
				break; 
			case SDLK_DOWN: 
				eventAction = END_MOVE_DOWN;
				break; 					
		}
	
	}

}

Action EventManager::getEventAction(){
	return eventAction;
}

Action EventManager::getRandomAction(){
	Action randomAction;
	switch(rand()%3){
		case 0:
			randomAction = JUMP_UP;
			break;
		case 1:
			randomAction = JUMP_LEFT;
			break;
		case 2:
			randomAction = JUMP_RIGHT;
			break;
		default:
			randomAction = JUMP_UP;
			break;			
	};
		
	return randomAction;
}